#!/bin/bash -xe

kata_coco_top_dir="$HOME/workspace/CoCo"

operator_repo_url="https://gitee.com/hanliyang-kata-coco/operator.git"
operator_repo_branch="0.12.0-hygon"
operator_repo_dir=${kata_coco_top_dir}/operator

if [ ! -e ${operator_repo_dir} ]; then
  git clone ${operator_repo_url} \
    -b ${operator_repo_branch} \
    ${operator_repo_dir}
fi

build_coco_operator_kata_deploy_csv_docker_image () {
  echo "Build CoCo operator's kata-deploy-csv docker image"
  pushd ${operator_repo_dir}/hygon

  kata_artifacts_tarball="kata-artifacts-csv.tgz"
  docker_image_tag="kata-deploy-csv:3.11.0"
  rm -rf opt/ || true
  rm -f ${kata_artifacts_tarball} || true
  sudo tar -zcf ${kata_artifacts_tarball} /opt/kata
  sudo chmod 777 ${kata_artifacts_tarball}
  tar -zxf ${kata_artifacts_tarball} -C .
  docker build \
    -t ${docker_image_tag} \
    -f Dockerfile.kata-deploy-csv \
    .
  docker save ${docker_image_tag} \
    > ${kata_coco_top_dir}/hygon-csv-kata-deploy.tar

  popd
}

build_coco_operator_kata_deploy_csv_docker_image
